package edu.bbu.commonproject.backend.logic.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * The password crypt and decrypt. 
 */
public class CryptingService
{
	/** The key for the encrypting. */
	private final byte[] key = "U$Q-[.#H:3B2o!pG".getBytes();
	
	public String encrypt(String strToEncrypt)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            String encryptedString = new String(Base64.encodeBase64(cipher.doFinal(strToEncrypt.getBytes())));
            return encryptedString;
        }
        catch (Exception e) 
        { 
        	return "EncryptError";
        }
    }
	
	
	public String decrypt(String strToDecrypt)
    {
		try
	    {
	        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
	        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
	        cipher.init(Cipher.DECRYPT_MODE, secretKey);
	        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
	        return decryptedString;
	    }
	    catch (Exception e) 
	    {
	    	return "DecryptError";
	    }
    }
}
